<?php
// +----------------------------------------------------------------------
// | 吉智云科技 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baijiahao.codewe.cn/admin.php All rights reserved.
// +----------------------------------------------------------------------
// | Author: 下雨吧จุ๊บ <642445992@qq.com>
// +----------------------------------------------------------------------
namespace Home\Controller;

use Think\Cache\Driver\Db;
use Common\Controller\Controller;

/**
 * 前台默认控制器
 * @author 下雨吧จุ๊บ <642445992@qq.com>
 */
class InterfaceController extends Controller
{

    private $data;
    /**
     * 构造方法
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function __construct()
    {
        if (!empty($_POST)){
            $this->data = $_POST;
        }
        if (!empty($_GET)){
            $this->data = (object)$_GET;
        }
        if (!empty(file_get_contents('php://input'))){
            $this->data = json_decode(file_get_contents('php://input'));
        }
        if (empty($_GET) && empty($_POST) && empty(file_get_contents('php://input'))){
            echo json_encode(array(
                'status' => 0,
                'message' => '请根据接口文档正确请求',
            ));exit;
        }
    }

    /**
     * 返回json数据
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function goBack($data)
    {
        //返回json数据
        echo json_encode($data);
        exit();
    }

    /**
     * 登录验证
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function login()
    {
        // 验证用户名密码是否正确
        $user_object = D('Admin/User');
        $user_info   = $user_object->login($this->data->username, $this->data->password);
        if (!$user_info) {
            $this->goBack(array(
                'status' => 0,
                'message' => $user_object->getError()
            ));
        }
        $user = D('admin_user');
        $where->username = $this->data->username;
        $where->status = 1;
        $member = $user->where($where)->find();
        $uid = $user_object->auto_login($user_info);
        //登录成功,返回用户信息
        $this->goBack(array(
            'status' => 1,
            'message' => $member
        ));
    }

    /**
     * 获取账号列表
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function accountList()
    {
        $start = ($this->data->page - 1) * $this->data->num;
        $end = $this->data->page * $this->data->num - 1;
        $zhanghao = D('account');
        $list = $zhanghao->select();
        if ($this->data->id == 144)
        {
            $wxstate['wxstate'] = 1;
            $list = $zhanghao->where($wxstate)->select();
        }
        //去除没有权限的账号 超级管理员拥有所有权限
        if ($this->data->id != 1){
            foreach ($list as $k=>$v) {
                $where1['aid'] = $v['id'];
                $where1['userid'] = $this->data->id;
                $account_user   = M('account_user');
                $account_user_info = $account_user
                    ->where($where1)
                    ->find();
                if (empty($account_user_info)){
                    unset($list[$k]);
                }
            }
        }
        //有账号返回账号,没有返回空
        if (empty($list)){
            $this->goBack(array(
                'status' => 0,
                'message' => '对应账号没有数据源'
            ));
        }else{
            $img = D('admin_upload');
            foreach ($list as $k=>$v) {
                $map->id = $v['image'];
                $list[$k]['image'] = 'http://jzyabc.cn'.$img->where($map)->find()['path'];
                $list[$k]['platname'] = D('platform')->find($v['platid'])['platname'];
            }
            array_multisort($list);
            foreach ($list as $k=>$v) {
                if ($k >= $start && $k <= $end){
                    $list1[$k] = $v;
                }
            }
            $this->goBack(array(
                'status' => 1,
                'count' => count($list),
                'message' => $list1
            ));
        }

    }


    /**
     * 获取前台账号cookie
     * @author Robert
     */
    public function acceptcookie()
    {
        $zhanghao = D('account');
        $data = $zhanghao->create();
        $data['id'] = $this->data->ids;
        $data['cookie'] = $this->data->cookies_data;
        $data['update_time'] = time();

        if ($data) {
            $result = $zhanghao
                ->field('id,update_time,cookie')
                ->save($data);
            if ($result) {
                $this->goBack(array(
                    'status' => 1,
                    'message' => '更新成功！'
                ));
            } else {
                $this->goBack(array(
                    'status' => 0,
                    'message' => '更新失败！'
                ));
            }
        } else {
            $this->goBack(array(
                'status' => 0,
                'message' => '数据错误！'
            ));
        }
    }

    /**
     * 获取cookie
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function cookie()
    {
        $zhanghao = D('account');
        $where->id = $this->data->id;
        $cookie = $zhanghao->where($where)->find()['cookie'];
        //有账号返回账号,没有返回空
        if (empty($cookie)){
            $this->goBack(array(
                'status' => 0,
                'message' => '对应账号没有数据源'
            ));
        }else{
            $this->goBack(array(
                'status' => 1,
                'message' => $cookie
            ));
        }
    }

    /**
     * 获取权限
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function menuList()
    {
        $id = $this->data->id;
        if ($id == 1){
            $menulist_data = $this->getMenu(0);
            foreach ($menulist_data as $key=>$v) {
                $menulist_data[$key]['id'] = $key;
                if (empty($v['grade'])){
                    $menulist_data[$key]['grade'] = 3;
                }
            }
            $menulist = $menulist_data;
        }else{
            $user_group = D('admin_access');
            $where->id = $id;
            $groupid = $user_group->where($where)->find()['id'];
            $menu = D('admin_group');
            $where->id = $groupid;
            $menulist = json_decode($menu->where($where)->find()['menu_auth'])->Admin;
            if (!empty($menulist)){
                foreach ($menulist as $k=>$v) {
                    $menulist[$k] = $this->getMenu($v);
                }
            }
        }
        if (!empty($menulist)){
            foreach ($menulist as $k=>$v) {
                if ($v['grade'] == 1){
                    $onelist[] = $v;
                }
            }
        }
        if (!empty($onelist)){
            foreach ($onelist as $k=>$v) {
                foreach ($menulist as $k1=>$v1) {
                    if ($v1['pid'] == $v['id']){
                        $onelist[$k]['child'][] = $v1;
                    }
                }
            }
        }
        $this->goBack(array(
            'status' => 1,
            'message' => $onelist
        ));
    }
}